﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Styx.Common;


namespace DruidUnstuckerFlight
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        #region Paths
        public static string HbFolder
        {
            get { return Path.GetDirectoryName(Application.ExecutablePath); }
        }

        public static string SettingsFolder
        {
            get { return Path.Combine(HbFolder, "Settings"); }
        }

        public static string DataFolder
        {
            get { return Path.Combine(HbFolder, "Data"); }
        }
        public static string PluginFolder
        {
            get { return Path.Combine(HbFolder, "Plugins"); }
        }
        public static string ThisPluginPath
        {
            get { return Path.Combine(PluginFolder, "Druid Flight Unstucker"); }
        }
        #endregion

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                Settings.Instance = ObjectXMLSerializer<Settings>.Load(Plugin.GlobalSettingsFile);
            }
            catch (Exception)
            {
                Settings.Instance = new Settings();
            }
            try
            {
                this.Icon = new Icon(Path.Combine(ThisPluginPath, "hawk.ico"));
            }
            catch
            {
                Logging.Write("Missing icon.");
            }
            Settings.Instance = new Settings();
            radiusTrack.Value = Settings.Instance.AntiStuckDistanceYards;
            heightTrack.Value = Settings.Instance.AntiStuckHeight;
            movementTimerTrack.Value = Settings.Instance.FlyUpSeconds;
            trackBar1.Value = Settings.Instance.CancelFlightFormSeconds;
            cancelLabel.Text = trackBar1.Value.ToString();
            heightLabel.Text = heightTrack.Value.ToString();
            moveLabel.Text = movementTimerTrack.Value.ToString();
            stuckYardsLabel.Text = radiusTrack.Value.ToString();
            yardsUpOrDownLabel.Text = heightTrack.Value < 0 ? "yards down" : "yards up";
            blackScreenCheckBox.Checked = Settings.Instance.DetectBlackscreen;
            reloadTimer.Text = Settings.Instance.ReloadUITimer.ToString();
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void radiusTrack_Scroll_1(object sender, EventArgs e)
        {
            toolTip1.SetToolTip(radiusTrack, radiusTrack.Value.ToString());
            stuckYardsLabel.Text = radiusTrack.Value.ToString();
            Settings.Instance.AntiStuckDistanceYards = radiusTrack.Value;
        }

        private void heightTrack_Scroll_1(object sender, EventArgs e)
        {
            yardsUpOrDownLabel.Text =  "yard" + (heightTrack.Value >= -1 && heightTrack.Value <= 1 ? " ":"s ");
            yardsUpOrDownLabel.Text += heightTrack.Value < 0 ? "up" : "down";
            toolTip1.SetToolTip(heightTrack, heightTrack.Value.ToString());
            heightLabel.Text = heightTrack.Value.ToString();
            Settings.Instance.AntiStuckHeight = heightTrack.Value;
        }

        private void movementTimerTrack_Scroll_1(object sender, EventArgs e)
        {
            toolTip1.SetToolTip(movementTimerTrack, movementTimerTrack.Value.ToString());
            moveLabel.Text = movementTimerTrack.Value.ToString();
            Settings.Instance.FlyUpSeconds = movementTimerTrack.Value;
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            toolTip1.SetToolTip(trackBar1, trackBar1.Value.ToString());
            cancelLabel.Text = trackBar1.Value.ToString();
            Settings.Instance.CancelFlightFormSeconds = trackBar1.Value;
        }

        private void blackScreenCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if(blackScreenCheckBox.Checked)
            {
                Settings.Instance.DetectBlackscreen = true;
                Size = new Size(410, 402);
            }
            else
            {
                Settings.Instance.DetectBlackscreen = false;
                Size = new Size(410, 323);
            }

        }

        private void trackBar2_Scroll(object sender, EventArgs e)
        {
            toolTip1.SetToolTip(trackBar2, trackBar2.Value.ToString());
            reloadTimer.Text = trackBar2.Value.ToString();
            Settings.Instance.CancelFlightFormSeconds = trackBar2.Value;
        }
    }
}
